{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<center><font color=gray size=6>Week06 基本图形_柱状图</font></center>\n",
    "<br/>\n",
    "<center><font color=gray size=4>2022.04.06</font></center>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 本周知识-柱状图\n",
    "## 基本柱状图绘制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "    <div class=\"bk-root\">\n",
       "        <a href=\"https://bokeh.org\" target=\"_blank\" class=\"bk-logo bk-logo-small bk-logo-notebook\"></a>\n",
       "        <span id=\"1002\">Loading BokehJS ...</span>\n",
       "    </div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "\n",
       "(function(root) {\n",
       "  function now() {\n",
       "    return new Date();\n",
       "  }\n",
       "\n",
       "  const force = true;\n",
       "\n",
       "  if (typeof root._bokeh_onload_callbacks === \"undefined\" || force === true) {\n",
       "    root._bokeh_onload_callbacks = [];\n",
       "    root._bokeh_is_loading = undefined;\n",
       "  }\n",
       "\n",
       "  const JS_MIME_TYPE = 'application/javascript';\n",
       "  const HTML_MIME_TYPE = 'text/html';\n",
       "  const EXEC_MIME_TYPE = 'application/vnd.bokehjs_exec.v0+json';\n",
       "  const CLASS_NAME = 'output_bokeh rendered_html';\n",
       "\n",
       "  /**\n",
       "   * Render data to the DOM node\n",
       "   */\n",
       "  function render(props, node) {\n",
       "    const script = document.createElement(\"script\");\n",
       "    node.appendChild(script);\n",
       "  }\n",
       "\n",
       "  /**\n",
       "   * Handle when an output is cleared or removed\n",
       "   */\n",
       "  function handleClearOutput(event, handle) {\n",
       "    const cell = handle.cell;\n",
       "\n",
       "    const id = cell.output_area._bokeh_element_id;\n",
       "    const server_id = cell.output_area._bokeh_server_id;\n",
       "    // Clean up Bokeh references\n",
       "    if (id != null && id in Bokeh.index) {\n",
       "      Bokeh.index[id].model.document.clear();\n",
       "      delete Bokeh.index[id];\n",
       "    }\n",
       "\n",
       "    if (server_id !== undefined) {\n",
       "      // Clean up Bokeh references\n",
       "      const cmd_clean = \"from bokeh.io.state import curstate; print(curstate().uuid_to_server['\" + server_id + \"'].get_sessions()[0].document.roots[0]._id)\";\n",
       "      cell.notebook.kernel.execute(cmd_clean, {\n",
       "        iopub: {\n",
       "          output: function(msg) {\n",
       "            const id = msg.content.text.trim();\n",
       "            if (id in Bokeh.index) {\n",
       "              Bokeh.index[id].model.document.clear();\n",
       "              delete Bokeh.index[id];\n",
       "            }\n",
       "          }\n",
       "        }\n",
       "      });\n",
       "      // Destroy server and session\n",
       "      const cmd_destroy = \"import bokeh.io.notebook as ion; ion.destroy_server('\" + server_id + \"')\";\n",
       "      cell.notebook.kernel.execute(cmd_destroy);\n",
       "    }\n",
       "  }\n",
       "\n",
       "  /**\n",
       "   * Handle when a new output is added\n",
       "   */\n",
       "  function handleAddOutput(event, handle) {\n",
       "    const output_area = handle.output_area;\n",
       "    const output = handle.output;\n",
       "\n",
       "    // limit handleAddOutput to display_data with EXEC_MIME_TYPE content only\n",
       "    if ((output.output_type != \"display_data\") || (!Object.prototype.hasOwnProperty.call(output.data, EXEC_MIME_TYPE))) {\n",
       "      return\n",
       "    }\n",
       "\n",
       "    const toinsert = output_area.element.find(\".\" + CLASS_NAME.split(' ')[0]);\n",
       "\n",
       "    if (output.metadata[EXEC_MIME_TYPE][\"id\"] !== undefined) {\n",
       "      toinsert[toinsert.length - 1].firstChild.textContent = output.data[JS_MIME_TYPE];\n",
       "      // store reference to embed id on output_area\n",
       "      output_area._bokeh_element_id = output.metadata[EXEC_MIME_TYPE][\"id\"];\n",
       "    }\n",
       "    if (output.metadata[EXEC_MIME_TYPE][\"server_id\"] !== undefined) {\n",
       "      const bk_div = document.createElement(\"div\");\n",
       "      bk_div.innerHTML = output.data[HTML_MIME_TYPE];\n",
       "      const script_attrs = bk_div.children[0].attributes;\n",
       "      for (let i = 0; i < script_attrs.length; i++) {\n",
       "        toinsert[toinsert.length - 1].firstChild.setAttribute(script_attrs[i].name, script_attrs[i].value);\n",
       "        toinsert[toinsert.length - 1].firstChild.textContent = bk_div.children[0].textContent\n",
       "      }\n",
       "      // store reference to server id on output_area\n",
       "      output_area._bokeh_server_id = output.metadata[EXEC_MIME_TYPE][\"server_id\"];\n",
       "    }\n",
       "  }\n",
       "\n",
       "  function register_renderer(events, OutputArea) {\n",
       "\n",
       "    function append_mime(data, metadata, element) {\n",
       "      // create a DOM node to render to\n",
       "      const toinsert = this.create_output_subarea(\n",
       "        metadata,\n",
       "        CLASS_NAME,\n",
       "        EXEC_MIME_TYPE\n",
       "      );\n",
       "      this.keyboard_manager.register_events(toinsert);\n",
       "      // Render to node\n",
       "      const props = {data: data, metadata: metadata[EXEC_MIME_TYPE]};\n",
       "      render(props, toinsert[toinsert.length - 1]);\n",
       "      element.append(toinsert);\n",
       "      return toinsert\n",
       "    }\n",
       "\n",
       "    /* Handle when an output is cleared or removed */\n",
       "    events.on('clear_output.CodeCell', handleClearOutput);\n",
       "    events.on('delete.Cell', handleClearOutput);\n",
       "\n",
       "    /* Handle when a new output is added */\n",
       "    events.on('output_added.OutputArea', handleAddOutput);\n",
       "\n",
       "    /**\n",
       "     * Register the mime type and append_mime function with output_area\n",
       "     */\n",
       "    OutputArea.prototype.register_mime_type(EXEC_MIME_TYPE, append_mime, {\n",
       "      /* Is output safe? */\n",
       "      safe: true,\n",
       "      /* Index of renderer in `output_area.display_order` */\n",
       "      index: 0\n",
       "    });\n",
       "  }\n",
       "\n",
       "  // register the mime type if in Jupyter Notebook environment and previously unregistered\n",
       "  if (root.Jupyter !== undefined) {\n",
       "    const events = require('base/js/events');\n",
       "    const OutputArea = require('notebook/js/outputarea').OutputArea;\n",
       "\n",
       "    if (OutputArea.prototype.mime_types().indexOf(EXEC_MIME_TYPE) == -1) {\n",
       "      register_renderer(events, OutputArea);\n",
       "    }\n",
       "  }\n",
       "\n",
       "  \n",
       "  if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n",
       "    root._bokeh_timeout = Date.now() + 5000;\n",
       "    root._bokeh_failed_load = false;\n",
       "  }\n",
       "\n",
       "  const NB_LOAD_WARNING = {'data': {'text/html':\n",
       "     \"<div style='background-color: #fdd'>\\n\"+\n",
       "     \"<p>\\n\"+\n",
       "     \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n",
       "     \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n",
       "     \"</p>\\n\"+\n",
       "     \"<ul>\\n\"+\n",
       "     \"<li>re-rerun `output_notebook()` to attempt to load from CDN again, or</li>\\n\"+\n",
       "     \"<li>use INLINE resources instead, as so:</li>\\n\"+\n",
       "     \"</ul>\\n\"+\n",
       "     \"<code>\\n\"+\n",
       "     \"from bokeh.resources import INLINE\\n\"+\n",
       "     \"output_notebook(resources=INLINE)\\n\"+\n",
       "     \"</code>\\n\"+\n",
       "     \"</div>\"}};\n",
       "\n",
       "  function display_loaded() {\n",
       "    const el = document.getElementById(\"1002\");\n",
       "    if (el != null) {\n",
       "      el.textContent = \"BokehJS is loading...\";\n",
       "    }\n",
       "    if (root.Bokeh !== undefined) {\n",
       "      if (el != null) {\n",
       "        el.textContent = \"BokehJS \" + root.Bokeh.version + \" successfully loaded.\";\n",
       "      }\n",
       "    } else if (Date.now() < root._bokeh_timeout) {\n",
       "      setTimeout(display_loaded, 100)\n",
       "    }\n",
       "  }\n",
       "\n",
       "\n",
       "  function run_callbacks() {\n",
       "    try {\n",
       "      root._bokeh_onload_callbacks.forEach(function(callback) {\n",
       "        if (callback != null)\n",
       "          callback();\n",
       "      });\n",
       "    } finally {\n",
       "      delete root._bokeh_onload_callbacks\n",
       "    }\n",
       "    console.debug(\"Bokeh: all callbacks have finished\");\n",
       "  }\n",
       "\n",
       "  function load_libs(css_urls, js_urls, callback) {\n",
       "    if (css_urls == null) css_urls = [];\n",
       "    if (js_urls == null) js_urls = [];\n",
       "\n",
       "    root._bokeh_onload_callbacks.push(callback);\n",
       "    if (root._bokeh_is_loading > 0) {\n",
       "      console.debug(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n",
       "      return null;\n",
       "    }\n",
       "    if (js_urls == null || js_urls.length === 0) {\n",
       "      run_callbacks();\n",
       "      return null;\n",
       "    }\n",
       "    console.debug(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n",
       "    root._bokeh_is_loading = css_urls.length + js_urls.length;\n",
       "\n",
       "    function on_load() {\n",
       "      root._bokeh_is_loading--;\n",
       "      if (root._bokeh_is_loading === 0) {\n",
       "        console.debug(\"Bokeh: all BokehJS libraries/stylesheets loaded\");\n",
       "        run_callbacks()\n",
       "      }\n",
       "    }\n",
       "\n",
       "    function on_error(url) {\n",
       "      console.error(\"failed to load \" + url);\n",
       "    }\n",
       "\n",
       "    for (let i = 0; i < css_urls.length; i++) {\n",
       "      const url = css_urls[i];\n",
       "      const element = document.createElement(\"link\");\n",
       "      element.onload = on_load;\n",
       "      element.onerror = on_error.bind(null, url);\n",
       "      element.rel = \"stylesheet\";\n",
       "      element.type = \"text/css\";\n",
       "      element.href = url;\n",
       "      console.debug(\"Bokeh: injecting link tag for BokehJS stylesheet: \", url);\n",
       "      document.body.appendChild(element);\n",
       "    }\n",
       "\n",
       "    for (let i = 0; i < js_urls.length; i++) {\n",
       "      const url = js_urls[i];\n",
       "      const element = document.createElement('script');\n",
       "      element.onload = on_load;\n",
       "      element.onerror = on_error.bind(null, url);\n",
       "      element.async = false;\n",
       "      element.src = url;\n",
       "      console.debug(\"Bokeh: injecting script tag for BokehJS library: \", url);\n",
       "      document.head.appendChild(element);\n",
       "    }\n",
       "  };\n",
       "\n",
       "  function inject_raw_css(css) {\n",
       "    const element = document.createElement(\"style\");\n",
       "    element.appendChild(document.createTextNode(css));\n",
       "    document.body.appendChild(element);\n",
       "  }\n",
       "\n",
       "  \n",
       "  const js_urls = [\"https://cdn.bokeh.org/bokeh/release/bokeh-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-gl-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-widgets-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-tables-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-mathjax-2.4.2.min.js\"];\n",
       "  const css_urls = [];\n",
       "  \n",
       "\n",
       "  const inline_js = [\n",
       "    function(Bokeh) {\n",
       "      Bokeh.set_log_level(\"info\");\n",
       "    },\n",
       "    function(Bokeh) {\n",
       "    \n",
       "    \n",
       "    }\n",
       "  ];\n",
       "\n",
       "  function run_inline_js() {\n",
       "    \n",
       "    if (root.Bokeh !== undefined || force === true) {\n",
       "      \n",
       "    for (let i = 0; i < inline_js.length; i++) {\n",
       "      inline_js[i].call(root, root.Bokeh);\n",
       "    }\n",
       "    if (force === true) {\n",
       "        display_loaded();\n",
       "      }} else if (Date.now() < root._bokeh_timeout) {\n",
       "      setTimeout(run_inline_js, 100);\n",
       "    } else if (!root._bokeh_failed_load) {\n",
       "      console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n",
       "      root._bokeh_failed_load = true;\n",
       "    } else if (force !== true) {\n",
       "      const cell = $(document.getElementById(\"1002\")).parents('.cell').data().cell;\n",
       "      cell.output_area.append_execute_result(NB_LOAD_WARNING)\n",
       "    }\n",
       "\n",
       "  }\n",
       "\n",
       "  if (root._bokeh_is_loading === 0) {\n",
       "    console.debug(\"Bokeh: BokehJS loaded, going straight to plotting\");\n",
       "    run_inline_js();\n",
       "  } else {\n",
       "    load_libs(css_urls, js_urls, function() {\n",
       "      console.debug(\"Bokeh: BokehJS plotting callback run at\", now());\n",
       "      run_inline_js();\n",
       "    });\n",
       "  }\n",
       "}(window));"
      ],
      "application/vnd.bokehjs_load.v0+json": "\n(function(root) {\n  function now() {\n    return new Date();\n  }\n\n  const force = true;\n\n  if (typeof root._bokeh_onload_callbacks === \"undefined\" || force === true) {\n    root._bokeh_onload_callbacks = [];\n    root._bokeh_is_loading = undefined;\n  }\n\n  \n\n  \n  if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n    root._bokeh_timeout = Date.now() + 5000;\n    root._bokeh_failed_load = false;\n  }\n\n  const NB_LOAD_WARNING = {'data': {'text/html':\n     \"<div style='background-color: #fdd'>\\n\"+\n     \"<p>\\n\"+\n     \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n     \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n     \"</p>\\n\"+\n     \"<ul>\\n\"+\n     \"<li>re-rerun `output_notebook()` to attempt to load from CDN again, or</li>\\n\"+\n     \"<li>use INLINE resources instead, as so:</li>\\n\"+\n     \"</ul>\\n\"+\n     \"<code>\\n\"+\n     \"from bokeh.resources import INLINE\\n\"+\n     \"output_notebook(resources=INLINE)\\n\"+\n     \"</code>\\n\"+\n     \"</div>\"}};\n\n  function display_loaded() {\n    const el = document.getElementById(\"1002\");\n    if (el != null) {\n      el.textContent = \"BokehJS is loading...\";\n    }\n    if (root.Bokeh !== undefined) {\n      if (el != null) {\n        el.textContent = \"BokehJS \" + root.Bokeh.version + \" successfully loaded.\";\n      }\n    } else if (Date.now() < root._bokeh_timeout) {\n      setTimeout(display_loaded, 100)\n    }\n  }\n\n\n  function run_callbacks() {\n    try {\n      root._bokeh_onload_callbacks.forEach(function(callback) {\n        if (callback != null)\n          callback();\n      });\n    } finally {\n      delete root._bokeh_onload_callbacks\n    }\n    console.debug(\"Bokeh: all callbacks have finished\");\n  }\n\n  function load_libs(css_urls, js_urls, callback) {\n    if (css_urls == null) css_urls = [];\n    if (js_urls == null) js_urls = [];\n\n    root._bokeh_onload_callbacks.push(callback);\n    if (root._bokeh_is_loading > 0) {\n      console.debug(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n      return null;\n    }\n    if (js_urls == null || js_urls.length === 0) {\n      run_callbacks();\n      return null;\n    }\n    console.debug(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n    root._bokeh_is_loading = css_urls.length + js_urls.length;\n\n    function on_load() {\n      root._bokeh_is_loading--;\n      if (root._bokeh_is_loading === 0) {\n        console.debug(\"Bokeh: all BokehJS libraries/stylesheets loaded\");\n        run_callbacks()\n      }\n    }\n\n    function on_error(url) {\n      console.error(\"failed to load \" + url);\n    }\n\n    for (let i = 0; i < css_urls.length; i++) {\n      const url = css_urls[i];\n      const element = document.createElement(\"link\");\n      element.onload = on_load;\n      element.onerror = on_error.bind(null, url);\n      element.rel = \"stylesheet\";\n      element.type = \"text/css\";\n      element.href = url;\n      console.debug(\"Bokeh: injecting link tag for BokehJS stylesheet: \", url);\n      document.body.appendChild(element);\n    }\n\n    for (let i = 0; i < js_urls.length; i++) {\n      const url = js_urls[i];\n      const element = document.createElement('script');\n      element.onload = on_load;\n      element.onerror = on_error.bind(null, url);\n      element.async = false;\n      element.src = url;\n      console.debug(\"Bokeh: injecting script tag for BokehJS library: \", url);\n      document.head.appendChild(element);\n    }\n  };\n\n  function inject_raw_css(css) {\n    const element = document.createElement(\"style\");\n    element.appendChild(document.createTextNode(css));\n    document.body.appendChild(element);\n  }\n\n  \n  const js_urls = [\"https://cdn.bokeh.org/bokeh/release/bokeh-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-gl-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-widgets-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-tables-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-mathjax-2.4.2.min.js\"];\n  const css_urls = [];\n  \n\n  const inline_js = [\n    function(Bokeh) {\n      Bokeh.set_log_level(\"info\");\n    },\n    function(Bokeh) {\n    \n    \n    }\n  ];\n\n  function run_inline_js() {\n    \n    if (root.Bokeh !== undefined || force === true) {\n      \n    for (let i = 0; i < inline_js.length; i++) {\n      inline_js[i].call(root, root.Bokeh);\n    }\n    if (force === true) {\n        display_loaded();\n      }} else if (Date.now() < root._bokeh_timeout) {\n      setTimeout(run_inline_js, 100);\n    } else if (!root._bokeh_failed_load) {\n      console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n      root._bokeh_failed_load = true;\n    } else if (force !== true) {\n      const cell = $(document.getElementById(\"1002\")).parents('.cell').data().cell;\n      cell.output_area.append_execute_result(NB_LOAD_WARNING)\n    }\n\n  }\n\n  if (root._bokeh_is_loading === 0) {\n    console.debug(\"Bokeh: BokehJS loaded, going straight to plotting\");\n    run_inline_js();\n  } else {\n    load_libs(css_urls, js_urls, function() {\n      console.debug(\"Bokeh: BokehJS plotting callback run at\", now());\n      run_inline_js();\n    });\n  }\n}(window));"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from bokeh.plotting import output_notebook,figure,show\n",
    "output_notebook()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 垂直柱状图\n",
    "* 绘制方法\n",
    "> vbar( )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 查看使用方法\n",
    "# help(p.vbar()) or ??p.vbar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "  <div class=\"bk-root\" id=\"641bdd74-fbc7-4363-a60b-458914ff6e24\" data-root-id=\"1003\"></div>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "(function(root) {\n",
       "  function embed_document(root) {\n",
       "    \n",
       "  const docs_json = {\"87fecab6-b797-496f-b488-f688d2cef60e\":{\"defs\":[],\"roots\":{\"references\":[{\"attributes\":{\"below\":[{\"id\":\"1012\"}],\"center\":[{\"id\":\"1015\"},{\"id\":\"1019\"}],\"height\":400,\"left\":[{\"id\":\"1016\"}],\"renderers\":[{\"id\":\"1038\"}],\"title\":{\"id\":\"1040\"},\"toolbar\":{\"id\":\"1027\"},\"width\":400,\"x_range\":{\"id\":\"1004\"},\"x_scale\":{\"id\":\"1008\"},\"y_range\":{\"id\":\"1006\"},\"y_scale\":{\"id\":\"1010\"}},\"id\":\"1003\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{\"tools\":[{\"id\":\"1020\"},{\"id\":\"1021\"},{\"id\":\"1022\"},{\"id\":\"1023\"},{\"id\":\"1024\"},{\"id\":\"1025\"}]},\"id\":\"1027\",\"type\":\"Toolbar\"},{\"attributes\":{},\"id\":\"1024\",\"type\":\"ResetTool\"},{\"attributes\":{\"coordinates\":null,\"formatter\":{\"id\":\"1043\"},\"group\":null,\"major_label_policy\":{\"id\":\"1044\"},\"ticker\":{\"id\":\"1017\"}},\"id\":\"1016\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"1044\",\"type\":\"AllLabels\"},{\"attributes\":{},\"id\":\"1047\",\"type\":\"AllLabels\"},{\"attributes\":{\"fill_color\":{\"value\":\"red\"},\"hatch_color\":{\"value\":\"red\"},\"line_color\":{\"value\":\"red\"},\"top\":{\"field\":\"top\"},\"width\":{\"value\":0.5},\"x\":{\"field\":\"x\"}},\"id\":\"1035\",\"type\":\"VBar\"},{\"attributes\":{\"data\":{\"top\":[1.2,2.5,3.7],\"x\":[1,2,3]},\"selected\":{\"id\":\"1049\"},\"selection_policy\":{\"id\":\"1048\"}},\"id\":\"1034\",\"type\":\"ColumnDataSource\"},{\"attributes\":{},\"id\":\"1046\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"bottom_units\":\"screen\",\"coordinates\":null,\"fill_alpha\":0.5,\"fill_color\":\"lightgrey\",\"group\":null,\"left_units\":\"screen\",\"level\":\"overlay\",\"line_alpha\":1.0,\"line_color\":\"black\",\"line_dash\":[4,4],\"line_width\":2,\"right_units\":\"screen\",\"syncable\":false,\"top_units\":\"screen\"},\"id\":\"1026\",\"type\":\"BoxAnnotation\"},{\"attributes\":{},\"id\":\"1004\",\"type\":\"DataRange1d\"},{\"attributes\":{},\"id\":\"1017\",\"type\":\"BasicTicker\"},{\"attributes\":{\"coordinates\":null,\"group\":null},\"id\":\"1040\",\"type\":\"Title\"},{\"attributes\":{\"coordinates\":null,\"data_source\":{\"id\":\"1034\"},\"glyph\":{\"id\":\"1035\"},\"group\":null,\"hover_glyph\":null,\"muted_glyph\":{\"id\":\"1037\"},\"nonselection_glyph\":{\"id\":\"1036\"},\"view\":{\"id\":\"1039\"}},\"id\":\"1038\",\"type\":\"GlyphRenderer\"},{\"attributes\":{},\"id\":\"1049\",\"type\":\"Selection\"},{\"attributes\":{},\"id\":\"1021\",\"type\":\"WheelZoomTool\"},{\"attributes\":{},\"id\":\"1043\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{},\"id\":\"1006\",\"type\":\"DataRange1d\"},{\"attributes\":{\"source\":{\"id\":\"1034\"}},\"id\":\"1039\",\"type\":\"CDSView\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"value\":\"red\"},\"hatch_alpha\":{\"value\":0.1},\"hatch_color\":{\"value\":\"red\"},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"red\"},\"top\":{\"field\":\"top\"},\"width\":{\"value\":0.5},\"x\":{\"field\":\"x\"}},\"id\":\"1036\",\"type\":\"VBar\"},{\"attributes\":{},\"id\":\"1048\",\"type\":\"UnionRenderers\"},{\"attributes\":{\"axis\":{\"id\":\"1016\"},\"coordinates\":null,\"dimension\":1,\"group\":null,\"ticker\":null},\"id\":\"1019\",\"type\":\"Grid\"},{\"attributes\":{\"coordinates\":null,\"formatter\":{\"id\":\"1046\"},\"group\":null,\"major_label_policy\":{\"id\":\"1047\"},\"ticker\":{\"id\":\"1013\"}},\"id\":\"1012\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"1020\",\"type\":\"PanTool\"},{\"attributes\":{},\"id\":\"1023\",\"type\":\"SaveTool\"},{\"attributes\":{\"overlay\":{\"id\":\"1026\"}},\"id\":\"1022\",\"type\":\"BoxZoomTool\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.2},\"fill_color\":{\"value\":\"red\"},\"hatch_alpha\":{\"value\":0.2},\"hatch_color\":{\"value\":\"red\"},\"line_alpha\":{\"value\":0.2},\"line_color\":{\"value\":\"red\"},\"top\":{\"field\":\"top\"},\"width\":{\"value\":0.5},\"x\":{\"field\":\"x\"}},\"id\":\"1037\",\"type\":\"VBar\"},{\"attributes\":{},\"id\":\"1010\",\"type\":\"LinearScale\"},{\"attributes\":{},\"id\":\"1008\",\"type\":\"LinearScale\"},{\"attributes\":{},\"id\":\"1025\",\"type\":\"HelpTool\"},{\"attributes\":{},\"id\":\"1013\",\"type\":\"BasicTicker\"},{\"attributes\":{\"axis\":{\"id\":\"1012\"},\"coordinates\":null,\"group\":null,\"ticker\":null},\"id\":\"1015\",\"type\":\"Grid\"}],\"root_ids\":[\"1003\"]},\"title\":\"Bokeh Application\",\"version\":\"2.4.2\"}};\n",
       "  const render_items = [{\"docid\":\"87fecab6-b797-496f-b488-f688d2cef60e\",\"root_ids\":[\"1003\"],\"roots\":{\"1003\":\"641bdd74-fbc7-4363-a60b-458914ff6e24\"}}];\n",
       "  root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\n",
       "\n",
       "  }\n",
       "  if (root.Bokeh !== undefined) {\n",
       "    embed_document(root);\n",
       "  } else {\n",
       "    let attempts = 0;\n",
       "    const timer = setInterval(function(root) {\n",
       "      if (root.Bokeh !== undefined) {\n",
       "        clearInterval(timer);\n",
       "        embed_document(root);\n",
       "      } else {\n",
       "        attempts++;\n",
       "        if (attempts > 100) {\n",
       "          clearInterval(timer);\n",
       "          console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\");\n",
       "        }\n",
       "      }\n",
       "    }, 10, root)\n",
       "  }\n",
       "})(window);"
      ],
      "application/vnd.bokehjs_exec.v0+json": ""
     },
     "metadata": {
      "application/vnd.bokehjs_exec.v0+json": {
       "id": "1003"
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 画布\n",
    "p = figure(plot_width=400, plot_height=400)\n",
    "p.vbar(x=[1, 2, 3], \n",
    "       width=0.5, \n",
    "       bottom=0,\n",
    "       top=[1.2, 2.5, 3.7], \n",
    "       color=\"red\")  # 垂直柱状图\n",
    "show(p)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 水平的柱状图\n",
    "* 绘制方法\n",
    "> hbar( )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 查看使用方法\n",
    "# help(p.hbar()) or ?? p.hbar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "  <div class=\"bk-root\" id=\"22a393fd-ffca-45bf-b82f-3f477a521186\" data-root-id=\"1106\"></div>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "(function(root) {\n",
       "  function embed_document(root) {\n",
       "    \n",
       "  const docs_json = {\"e06132e0-7aea-4879-8835-8ec5100ec75e\":{\"defs\":[],\"roots\":{\"references\":[{\"attributes\":{\"below\":[{\"id\":\"1117\"}],\"center\":[{\"id\":\"1120\"},{\"id\":\"1124\"}],\"height\":200,\"left\":[{\"id\":\"1121\"}],\"renderers\":[{\"id\":\"1143\"}],\"title\":{\"id\":\"1107\"},\"toolbar\":{\"id\":\"1132\"},\"width\":400,\"x_range\":{\"id\":\"1109\"},\"x_scale\":{\"id\":\"1113\"},\"y_range\":{\"id\":\"1111\"},\"y_scale\":{\"id\":\"1115\"}},\"id\":\"1106\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{\"bottom_units\":\"screen\",\"coordinates\":null,\"fill_alpha\":0.5,\"fill_color\":\"lightgrey\",\"group\":null,\"left_units\":\"screen\",\"level\":\"overlay\",\"line_alpha\":1.0,\"line_color\":\"black\",\"line_dash\":[4,4],\"line_width\":2,\"right_units\":\"screen\",\"syncable\":false,\"top_units\":\"screen\"},\"id\":\"1131\",\"type\":\"BoxAnnotation\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"value\":\"navy\"},\"hatch_alpha\":{\"value\":0.1},\"hatch_color\":{\"value\":\"navy\"},\"height\":{\"value\":0.25},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"navy\"},\"right\":{\"field\":\"right\"},\"y\":{\"field\":\"y\"}},\"id\":\"1141\",\"type\":\"HBar\"},{\"attributes\":{\"tools\":[{\"id\":\"1125\"},{\"id\":\"1126\"},{\"id\":\"1127\"},{\"id\":\"1128\"},{\"id\":\"1129\"},{\"id\":\"1130\"}]},\"id\":\"1132\",\"type\":\"Toolbar\"},{\"attributes\":{\"data\":{\"right\":[1.2,2.5,3.7],\"y\":[1,2,3]},\"selected\":{\"id\":\"1164\"},\"selection_policy\":{\"id\":\"1163\"}},\"id\":\"1139\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"source\":{\"id\":\"1139\"}},\"id\":\"1144\",\"type\":\"CDSView\"},{\"attributes\":{},\"id\":\"1163\",\"type\":\"UnionRenderers\"},{\"attributes\":{},\"id\":\"1126\",\"type\":\"WheelZoomTool\"},{\"attributes\":{},\"id\":\"1128\",\"type\":\"SaveTool\"},{\"attributes\":{},\"id\":\"1129\",\"type\":\"ResetTool\"},{\"attributes\":{\"axis\":{\"id\":\"1121\"},\"coordinates\":null,\"dimension\":1,\"group\":null,\"ticker\":null},\"id\":\"1124\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"1130\",\"type\":\"HelpTool\"},{\"attributes\":{},\"id\":\"1113\",\"type\":\"LinearScale\"},{\"attributes\":{},\"id\":\"1122\",\"type\":\"BasicTicker\"},{\"attributes\":{},\"id\":\"1158\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"fill_color\":{\"value\":\"navy\"},\"hatch_color\":{\"value\":\"navy\"},\"height\":{\"value\":0.25},\"line_color\":{\"value\":\"navy\"},\"right\":{\"field\":\"right\"},\"y\":{\"field\":\"y\"}},\"id\":\"1140\",\"type\":\"HBar\"},{\"attributes\":{},\"id\":\"1118\",\"type\":\"BasicTicker\"},{\"attributes\":{},\"id\":\"1161\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.2},\"fill_color\":{\"value\":\"navy\"},\"hatch_alpha\":{\"value\":0.2},\"hatch_color\":{\"value\":\"navy\"},\"height\":{\"value\":0.25},\"line_alpha\":{\"value\":0.2},\"line_color\":{\"value\":\"navy\"},\"right\":{\"field\":\"right\"},\"y\":{\"field\":\"y\"}},\"id\":\"1142\",\"type\":\"HBar\"},{\"attributes\":{\"coordinates\":null,\"data_source\":{\"id\":\"1139\"},\"glyph\":{\"id\":\"1140\"},\"group\":null,\"hover_glyph\":null,\"muted_glyph\":{\"id\":\"1142\"},\"nonselection_glyph\":{\"id\":\"1141\"},\"view\":{\"id\":\"1144\"}},\"id\":\"1143\",\"type\":\"GlyphRenderer\"},{\"attributes\":{},\"id\":\"1125\",\"type\":\"PanTool\"},{\"attributes\":{},\"id\":\"1162\",\"type\":\"AllLabels\"},{\"attributes\":{\"coordinates\":null,\"formatter\":{\"id\":\"1158\"},\"group\":null,\"major_label_policy\":{\"id\":\"1159\"},\"ticker\":{\"id\":\"1122\"}},\"id\":\"1121\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"1109\",\"type\":\"DataRange1d\"},{\"attributes\":{},\"id\":\"1115\",\"type\":\"LinearScale\"},{\"attributes\":{\"axis\":{\"id\":\"1117\"},\"coordinates\":null,\"group\":null,\"ticker\":null},\"id\":\"1120\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"1159\",\"type\":\"AllLabels\"},{\"attributes\":{\"overlay\":{\"id\":\"1131\"}},\"id\":\"1127\",\"type\":\"BoxZoomTool\"},{\"attributes\":{\"coordinates\":null,\"group\":null,\"text\":\"\\u6c34\\u5e73\\u67f1\\u72b6\\u56fe\"},\"id\":\"1107\",\"type\":\"Title\"},{\"attributes\":{},\"id\":\"1164\",\"type\":\"Selection\"},{\"attributes\":{\"coordinates\":null,\"formatter\":{\"id\":\"1161\"},\"group\":null,\"major_label_policy\":{\"id\":\"1162\"},\"ticker\":{\"id\":\"1118\"}},\"id\":\"1117\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"1111\",\"type\":\"DataRange1d\"}],\"root_ids\":[\"1106\"]},\"title\":\"Bokeh Application\",\"version\":\"2.4.2\"}};\n",
       "  const render_items = [{\"docid\":\"e06132e0-7aea-4879-8835-8ec5100ec75e\",\"root_ids\":[\"1106\"],\"roots\":{\"1106\":\"22a393fd-ffca-45bf-b82f-3f477a521186\"}}];\n",
       "  root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\n",
       "\n",
       "  }\n",
       "  if (root.Bokeh !== undefined) {\n",
       "    embed_document(root);\n",
       "  } else {\n",
       "    let attempts = 0;\n",
       "    const timer = setInterval(function(root) {\n",
       "      if (root.Bokeh !== undefined) {\n",
       "        clearInterval(timer);\n",
       "        embed_document(root);\n",
       "      } else {\n",
       "        attempts++;\n",
       "        if (attempts > 100) {\n",
       "          clearInterval(timer);\n",
       "          console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\");\n",
       "        }\n",
       "      }\n",
       "    }, 10, root)\n",
       "  }\n",
       "})(window);"
      ],
      "application/vnd.bokehjs_exec.v0+json": ""
     },
     "metadata": {
      "application/vnd.bokehjs_exec.v0+json": {
       "id": "1106"
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "p = figure(plot_width=400, plot_height=200,title=\"水平柱状图\")\n",
    "p.hbar(y=[1, 2, 3], \n",
    "       height=0.25, \n",
    "       left=0,\n",
    "       right=[1.2, 2.5, 3.7], \n",
    "       color=\"navy\")  # 水平柱状图\n",
    "show(p)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 演示1. 水果数量锤子柱状图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "  <div class=\"bk-root\" id=\"bae4ad02-6932-4ee2-8212-1adda428dc8a\" data-root-id=\"1217\"></div>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "(function(root) {\n",
       "  function embed_document(root) {\n",
       "    \n",
       "  const docs_json = {\"ba63a7cc-1aad-4177-ac17-5102c559f5c4\":{\"defs\":[],\"roots\":{\"references\":[{\"attributes\":{\"below\":[{\"id\":\"1228\"}],\"center\":[{\"id\":\"1230\"},{\"id\":\"1234\"}],\"height\":350,\"left\":[{\"id\":\"1231\"}],\"renderers\":[{\"id\":\"1255\"}],\"title\":{\"id\":\"1218\"},\"toolbar\":{\"id\":\"1243\"},\"x_range\":{\"id\":\"1220\"},\"x_scale\":{\"id\":\"1224\"},\"y_range\":{\"id\":\"1222\"},\"y_scale\":{\"id\":\"1226\"}},\"id\":\"1217\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{\"axis\":{\"id\":\"1228\"},\"coordinates\":null,\"grid_line_color\":null,\"group\":null,\"ticker\":null},\"id\":\"1230\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"1236\",\"type\":\"WheelZoomTool\"},{\"attributes\":{},\"id\":\"1229\",\"type\":\"CategoricalTicker\"},{\"attributes\":{},\"id\":\"1232\",\"type\":\"BasicTicker\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.2},\"fill_color\":{\"value\":\"orange\"},\"hatch_alpha\":{\"value\":0.2},\"hatch_color\":{\"value\":\"orange\"},\"line_alpha\":{\"value\":0.2},\"line_color\":{\"value\":\"orange\"},\"top\":{\"field\":\"counts\"},\"width\":{\"value\":0.85},\"x\":{\"field\":\"x\"}},\"id\":\"1254\",\"type\":\"VBar\"},{\"attributes\":{\"factors\":[\"Apples\",\"Pears\",\"Nectarines\",\"Plums\",\"Grapes\",\"Strawberries\"]},\"id\":\"1220\",\"type\":\"FactorRange\"},{\"attributes\":{\"data\":{\"counts\":[5,3,4,2,4,6],\"x\":[\"Apples\",\"Pears\",\"Nectarines\",\"Plums\",\"Grapes\",\"Strawberries\"]},\"selected\":{\"id\":\"1286\"},\"selection_policy\":{\"id\":\"1285\"}},\"id\":\"1216\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"coordinates\":null,\"formatter\":{\"id\":\"1283\"},\"group\":null,\"major_label_policy\":{\"id\":\"1284\"},\"ticker\":{\"id\":\"1229\"}},\"id\":\"1228\",\"type\":\"CategoricalAxis\"},{\"attributes\":{},\"id\":\"1224\",\"type\":\"CategoricalScale\"},{\"attributes\":{\"coordinates\":null,\"formatter\":{\"id\":\"1280\"},\"group\":null,\"major_label_policy\":{\"id\":\"1281\"},\"ticker\":{\"id\":\"1232\"}},\"id\":\"1231\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"1285\",\"type\":\"UnionRenderers\"},{\"attributes\":{},\"id\":\"1226\",\"type\":\"LinearScale\"},{\"attributes\":{\"tools\":[{\"id\":\"1235\"},{\"id\":\"1236\"},{\"id\":\"1237\"},{\"id\":\"1238\"},{\"id\":\"1239\"},{\"id\":\"1240\"},{\"id\":\"1242\"}]},\"id\":\"1243\",\"type\":\"Toolbar\"},{\"attributes\":{\"coordinates\":null,\"group\":null,\"text\":\"Fruit Counts\"},\"id\":\"1218\",\"type\":\"Title\"},{\"attributes\":{},\"id\":\"1284\",\"type\":\"AllLabels\"},{\"attributes\":{\"callback\":null,\"tooltips\":[[\"\\u6c34\\u679c\\u540d\\u79f0\",\"@x\"],[\"\\u6570\\u91cf\",\"@counts\"]]},\"id\":\"1242\",\"type\":\"HoverTool\"},{\"attributes\":{},\"id\":\"1238\",\"type\":\"SaveTool\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"value\":\"orange\"},\"hatch_alpha\":{\"value\":0.1},\"hatch_color\":{\"value\":\"orange\"},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"orange\"},\"top\":{\"field\":\"counts\"},\"width\":{\"value\":0.85},\"x\":{\"field\":\"x\"}},\"id\":\"1253\",\"type\":\"VBar\"},{\"attributes\":{},\"id\":\"1280\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"start\":0},\"id\":\"1222\",\"type\":\"DataRange1d\"},{\"attributes\":{},\"id\":\"1239\",\"type\":\"ResetTool\"},{\"attributes\":{},\"id\":\"1240\",\"type\":\"HelpTool\"},{\"attributes\":{\"source\":{\"id\":\"1216\"}},\"id\":\"1256\",\"type\":\"CDSView\"},{\"attributes\":{\"fill_color\":{\"value\":\"orange\"},\"hatch_color\":{\"value\":\"orange\"},\"line_color\":{\"value\":\"orange\"},\"top\":{\"field\":\"counts\"},\"width\":{\"value\":0.85},\"x\":{\"field\":\"x\"}},\"id\":\"1252\",\"type\":\"VBar\"},{\"attributes\":{\"axis\":{\"id\":\"1231\"},\"coordinates\":null,\"dimension\":1,\"group\":null,\"ticker\":null},\"id\":\"1234\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"1286\",\"type\":\"Selection\"},{\"attributes\":{\"overlay\":{\"id\":\"1241\"}},\"id\":\"1237\",\"type\":\"BoxZoomTool\"},{\"attributes\":{},\"id\":\"1283\",\"type\":\"CategoricalTickFormatter\"},{\"attributes\":{},\"id\":\"1235\",\"type\":\"PanTool\"},{\"attributes\":{\"bottom_units\":\"screen\",\"coordinates\":null,\"fill_alpha\":0.5,\"fill_color\":\"lightgrey\",\"group\":null,\"left_units\":\"screen\",\"level\":\"overlay\",\"line_alpha\":1.0,\"line_color\":\"black\",\"line_dash\":[4,4],\"line_width\":2,\"right_units\":\"screen\",\"syncable\":false,\"top_units\":\"screen\"},\"id\":\"1241\",\"type\":\"BoxAnnotation\"},{\"attributes\":{},\"id\":\"1281\",\"type\":\"AllLabels\"},{\"attributes\":{\"coordinates\":null,\"data_source\":{\"id\":\"1216\"},\"glyph\":{\"id\":\"1252\"},\"group\":null,\"hover_glyph\":null,\"muted_glyph\":{\"id\":\"1254\"},\"nonselection_glyph\":{\"id\":\"1253\"},\"view\":{\"id\":\"1256\"}},\"id\":\"1255\",\"type\":\"GlyphRenderer\"}],\"root_ids\":[\"1217\"]},\"title\":\"Bokeh Application\",\"version\":\"2.4.2\"}};\n",
       "  const render_items = [{\"docid\":\"ba63a7cc-1aad-4177-ac17-5102c559f5c4\",\"root_ids\":[\"1217\"],\"roots\":{\"1217\":\"bae4ad02-6932-4ee2-8212-1adda428dc8a\"}}];\n",
       "  root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\n",
       "\n",
       "  }\n",
       "  if (root.Bokeh !== undefined) {\n",
       "    embed_document(root);\n",
       "  } else {\n",
       "    let attempts = 0;\n",
       "    const timer = setInterval(function(root) {\n",
       "      if (root.Bokeh !== undefined) {\n",
       "        clearInterval(timer);\n",
       "        embed_document(root);\n",
       "      } else {\n",
       "        attempts++;\n",
       "        if (attempts > 100) {\n",
       "          clearInterval(timer);\n",
       "          console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\");\n",
       "        }\n",
       "      }\n",
       "    }, 10, root)\n",
       "  }\n",
       "})(window);"
      ],
      "application/vnd.bokehjs_exec.v0+json": ""
     },
     "metadata": {
      "application/vnd.bokehjs_exec.v0+json": {
       "id": "1217"
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from bokeh.models import ColumnDataSource\n",
    "# 数据准备 (名称和值)\n",
    "fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']\n",
    "counts = [5, 3, 4, 2, 4, 6]\n",
    "source = ColumnDataSource(data=dict(\n",
    "    x=fruits,\n",
    "    counts=counts\n",
    "    ))\n",
    "#交互\n",
    "TOOLTIPS = [\n",
    "    (\"水果名称\", '@x'),\n",
    "    (\"数量\", \"@counts\")\n",
    "]\n",
    "# 画布\n",
    "p = figure(x_range=fruits, \n",
    "           plot_height=350, \n",
    "           title=\"Fruit Counts\",\n",
    "           tooltips=TOOLTIPS\n",
    "         )\n",
    "# 绘制\n",
    "p.vbar(x='x', \n",
    "       top='counts', \n",
    "       width=0.85,\n",
    "      source=source,\n",
    "      color=\"orange\")\n",
    "\n",
    "p.xgrid.grid_line_color = None\n",
    "p.y_range.start = 0\n",
    "# 显示\n",
    "show(p)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 实践2 数据源链接[数据源](https://gitee.com/xzhichao/interactive-data-visualization/blob/master/week06/data/visualization-20190505.csv)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><div id=776336f9-0788-47ca-a1ab-3a2fc8ed9a69 style=\"display:none; background-color:#9D6CFF; color:white; width:200px; height:30px; padding-left:5px; border-radius:4px; flex-direction:row; justify-content:space-around; align-items:center;\" onmouseover=\"this.style.backgroundColor='#BA9BF8'\" onmouseout=\"this.style.backgroundColor='#9D6CFF'\" onclick=\"window.commands?.execute('create-mitosheet-from-dataframe-output');\">Open in Mito</div> <script> if (window.commands.hasCommand('create-mitosheet-from-dataframe-output')) document.getElementById('776336f9-0788-47ca-a1ab-3a2fc8ed9a69').style.display = 'flex' </script> <table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Visualization_tools</th>\n",
       "      <th>Watch</th>\n",
       "      <th>Star</th>\n",
       "      <th>Fork</th>\n",
       "      <th>Commits</th>\n",
       "      <th>Contributors</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>matplotlib</td>\n",
       "      <td>533</td>\n",
       "      <td>9678</td>\n",
       "      <td>4143</td>\n",
       "      <td>29503</td>\n",
       "      <td>808</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>bokeh</td>\n",
       "      <td>396</td>\n",
       "      <td>11034</td>\n",
       "      <td>2526</td>\n",
       "      <td>17673</td>\n",
       "      <td>357</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>pyecharts</td>\n",
       "      <td>263</td>\n",
       "      <td>5387</td>\n",
       "      <td>1148</td>\n",
       "      <td>1321</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>seaborn</td>\n",
       "      <td>234</td>\n",
       "      <td>6038</td>\n",
       "      <td>970</td>\n",
       "      <td>2316</td>\n",
       "      <td>98</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>plotly</td>\n",
       "      <td>234</td>\n",
       "      <td>4928</td>\n",
       "      <td>1114</td>\n",
       "      <td>3370</td>\n",
       "      <td>76</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>ggplot2</td>\n",
       "      <td>329</td>\n",
       "      <td>3783</td>\n",
       "      <td>1427</td>\n",
       "      <td>4286</td>\n",
       "      <td>184</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table></div>"
      ],
      "text/plain": [
       "  Visualization_tools  Watch   Star  Fork  Commits  Contributors\n",
       "0          matplotlib    533   9678  4143    29503           808\n",
       "1               bokeh    396  11034  2526    17673           357\n",
       "2           pyecharts    263   5387  1148     1321            18\n",
       "3             seaborn    234   6038   970     2316            98\n",
       "4              plotly    234   4928  1114     3370            76\n",
       "5             ggplot2    329   3783  1427     4286           184"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据处理\n",
    "import pandas as pd\n",
    "df=pd.read_csv('week6_data.csv')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     9678\n",
       "1    11034\n",
       "2     5387\n",
       "3     6038\n",
       "4     4928\n",
       "5     3783\n",
       "Name: Star, dtype: int64"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "0    matplotlib\n",
       "1         bokeh\n",
       "2     pyecharts\n",
       "3       seaborn\n",
       "4        plotly\n",
       "5       ggplot2\n",
       "Name: Visualization_tools, dtype: object"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(df['Star'])\n",
    "display(df['Visualization_tools'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "BokehDeprecationWarning: 'legend' keyword is deprecated, use explicit 'legend_label', 'legend_field', or 'legend_group' keywords instead\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "  <div class=\"bk-root\" id=\"457abdbb-c05f-4385-bbbd-5b70280d3e10\" data-root-id=\"1339\"></div>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "(function(root) {\n",
       "  function embed_document(root) {\n",
       "    \n",
       "  const docs_json = {\"5033a55f-6932-4aba-81c9-23a5cda9cdd2\":{\"defs\":[],\"roots\":{\"references\":[{\"attributes\":{\"below\":[{\"id\":\"1350\"}],\"center\":[{\"id\":\"1352\"},{\"id\":\"1356\"},{\"id\":\"1389\"}],\"height\":450,\"left\":[{\"id\":\"1353\"}],\"renderers\":[{\"id\":\"1377\"}],\"title\":{\"id\":\"1340\"},\"toolbar\":{\"id\":\"1365\"},\"x_range\":{\"id\":\"1342\"},\"x_scale\":{\"id\":\"1346\"},\"y_range\":{\"id\":\"1344\"},\"y_scale\":{\"id\":\"1348\"}},\"id\":\"1339\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{\"source\":{\"id\":\"1338\"}},\"id\":\"1378\",\"type\":\"CDSView\"},{\"attributes\":{\"coordinates\":null,\"group\":null,\"text\":\"2019\\u5e74\\u53ef\\u89c6\\u5316\\u5de5\\u5177\\u6807\\u661f\\u6570\\u91cf\"},\"id\":\"1340\",\"type\":\"Title\"},{\"attributes\":{\"axis\":{\"id\":\"1353\"},\"coordinates\":null,\"dimension\":1,\"group\":null,\"ticker\":null},\"id\":\"1356\",\"type\":\"Grid\"},{\"attributes\":{\"fill_color\":{\"field\":\"color\"},\"hatch_color\":{\"field\":\"color\"},\"line_color\":{\"field\":\"color\"},\"top\":{\"field\":\"counts\"},\"width\":{\"value\":0.85},\"x\":{\"field\":\"x\"}},\"id\":\"1374\",\"type\":\"VBar\"},{\"attributes\":{},\"id\":\"1386\",\"type\":\"UnionRenderers\"},{\"attributes\":{\"start\":0},\"id\":\"1344\",\"type\":\"DataRange1d\"},{\"attributes\":{},\"id\":\"1346\",\"type\":\"CategoricalScale\"},{\"attributes\":{\"coordinates\":null,\"formatter\":{\"id\":\"1381\"},\"group\":null,\"major_label_policy\":{\"id\":\"1382\"},\"ticker\":{\"id\":\"1354\"}},\"id\":\"1353\",\"type\":\"LinearAxis\"},{\"attributes\":{\"label\":{\"field\":\"x\"},\"renderers\":[{\"id\":\"1377\"}]},\"id\":\"1390\",\"type\":\"LegendItem\"},{\"attributes\":{\"overlay\":{\"id\":\"1363\"}},\"id\":\"1359\",\"type\":\"BoxZoomTool\"},{\"attributes\":{\"axis\":{\"id\":\"1350\"},\"coordinates\":null,\"grid_line_color\":null,\"group\":null,\"ticker\":null},\"id\":\"1352\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"1387\",\"type\":\"Selection\"},{\"attributes\":{},\"id\":\"1384\",\"type\":\"CategoricalTickFormatter\"},{\"attributes\":{},\"id\":\"1357\",\"type\":\"PanTool\"},{\"attributes\":{},\"id\":\"1358\",\"type\":\"WheelZoomTool\"},{\"attributes\":{\"factors\":[\"matplotlib\",\"bokeh\",\"pyecharts\",\"seaborn\",\"plotly\",\"ggplot2\"]},\"id\":\"1342\",\"type\":\"FactorRange\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.2},\"fill_color\":{\"field\":\"color\"},\"hatch_alpha\":{\"value\":0.2},\"hatch_color\":{\"field\":\"color\"},\"line_alpha\":{\"value\":0.2},\"line_color\":{\"field\":\"color\"},\"top\":{\"field\":\"counts\"},\"width\":{\"value\":0.85},\"x\":{\"field\":\"x\"}},\"id\":\"1376\",\"type\":\"VBar\"},{\"attributes\":{},\"id\":\"1360\",\"type\":\"SaveTool\"},{\"attributes\":{\"coordinates\":null,\"data_source\":{\"id\":\"1338\"},\"glyph\":{\"id\":\"1374\"},\"group\":null,\"hover_glyph\":null,\"muted_glyph\":{\"id\":\"1376\"},\"nonselection_glyph\":{\"id\":\"1375\"},\"view\":{\"id\":\"1378\"}},\"id\":\"1377\",\"type\":\"GlyphRenderer\"},{\"attributes\":{},\"id\":\"1382\",\"type\":\"AllLabels\"},{\"attributes\":{},\"id\":\"1385\",\"type\":\"AllLabels\"},{\"attributes\":{\"coordinates\":null,\"group\":null,\"items\":[{\"id\":\"1390\"}],\"location\":\"top_center\",\"orientation\":\"horizontal\"},\"id\":\"1389\",\"type\":\"Legend\"},{\"attributes\":{},\"id\":\"1354\",\"type\":\"BasicTicker\"},{\"attributes\":{},\"id\":\"1348\",\"type\":\"LinearScale\"},{\"attributes\":{\"coordinates\":null,\"formatter\":{\"id\":\"1384\"},\"group\":null,\"major_label_policy\":{\"id\":\"1385\"},\"ticker\":{\"id\":\"1351\"}},\"id\":\"1350\",\"type\":\"CategoricalAxis\"},{\"attributes\":{},\"id\":\"1362\",\"type\":\"HelpTool\"},{\"attributes\":{},\"id\":\"1381\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"callback\":null,\"tooltips\":[[\"\\u53ef\\u89c6\\u5316\\u5de5\\u5177\\u540d\\u79f0\",\"@x\"],[\"star\\u6570\\u91cf\",\"@counts\"]]},\"id\":\"1364\",\"type\":\"HoverTool\"},{\"attributes\":{\"tools\":[{\"id\":\"1357\"},{\"id\":\"1358\"},{\"id\":\"1359\"},{\"id\":\"1360\"},{\"id\":\"1361\"},{\"id\":\"1362\"},{\"id\":\"1364\"}]},\"id\":\"1365\",\"type\":\"Toolbar\"},{\"attributes\":{\"data\":{\"color\":[\"#3288bd\",\"#99d594\",\"#e6f598\",\"#fee08b\",\"#fc8d59\",\"#d53e4f\"],\"counts\":[9678,11034,5387,6038,4928,3783],\"x\":[\"matplotlib\",\"bokeh\",\"pyecharts\",\"seaborn\",\"plotly\",\"ggplot2\"]},\"selected\":{\"id\":\"1387\"},\"selection_policy\":{\"id\":\"1386\"}},\"id\":\"1338\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"field\":\"color\"},\"hatch_alpha\":{\"value\":0.1},\"hatch_color\":{\"field\":\"color\"},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"field\":\"color\"},\"top\":{\"field\":\"counts\"},\"width\":{\"value\":0.85},\"x\":{\"field\":\"x\"}},\"id\":\"1375\",\"type\":\"VBar\"},{\"attributes\":{},\"id\":\"1361\",\"type\":\"ResetTool\"},{\"attributes\":{\"bottom_units\":\"screen\",\"coordinates\":null,\"fill_alpha\":0.5,\"fill_color\":\"lightgrey\",\"group\":null,\"left_units\":\"screen\",\"level\":\"overlay\",\"line_alpha\":1.0,\"line_color\":\"black\",\"line_dash\":[4,4],\"line_width\":2,\"right_units\":\"screen\",\"syncable\":false,\"top_units\":\"screen\"},\"id\":\"1363\",\"type\":\"BoxAnnotation\"},{\"attributes\":{},\"id\":\"1351\",\"type\":\"CategoricalTicker\"}],\"root_ids\":[\"1339\"]},\"title\":\"Bokeh Application\",\"version\":\"2.4.2\"}};\n",
       "  const render_items = [{\"docid\":\"5033a55f-6932-4aba-81c9-23a5cda9cdd2\",\"root_ids\":[\"1339\"],\"roots\":{\"1339\":\"457abdbb-c05f-4385-bbbd-5b70280d3e10\"}}];\n",
       "  root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\n",
       "\n",
       "  }\n",
       "  if (root.Bokeh !== undefined) {\n",
       "    embed_document(root);\n",
       "  } else {\n",
       "    let attempts = 0;\n",
       "    const timer = setInterval(function(root) {\n",
       "      if (root.Bokeh !== undefined) {\n",
       "        clearInterval(timer);\n",
       "        embed_document(root);\n",
       "      } else {\n",
       "        attempts++;\n",
       "        if (attempts > 100) {\n",
       "          clearInterval(timer);\n",
       "          console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\");\n",
       "        }\n",
       "      }\n",
       "    }, 10, root)\n",
       "  }\n",
       "})(window);"
      ],
      "application/vnd.bokehjs_exec.v0+json": ""
     },
     "metadata": {
      "application/vnd.bokehjs_exec.v0+json": {
       "id": "1339"
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from bokeh.models import ColumnDataSource\n",
    "from bokeh.palettes import Spectral6\n",
    "# 数据处理\n",
    "source = ColumnDataSource(data=dict(\n",
    "    x=df['Visualization_tools'],\n",
    "    counts=df['Star'],\n",
    "    color=Spectral6\n",
    "    ))\n",
    "#交互\n",
    "TOOLTIPS = [\n",
    "    (\"可视化工具名称\", '@x'),\n",
    "    (\"star数量\", \"@counts\")\n",
    "]\n",
    "\n",
    "p = figure(x_range=df['Visualization_tools'],\n",
    "           plot_height=450,\n",
    "           title=\"2019年可视化工具标星数量\",\n",
    "          tooltips=TOOLTIPS)\n",
    "\n",
    "p.vbar(x='x', \n",
    "       top='counts', \n",
    "       color=\"color\",\n",
    "       width=0.85,\n",
    "      source=source,\n",
    "      legend=\"x\")\n",
    "\n",
    "p.xgrid.grid_line_color = None\n",
    "p.y_range.start = 0\n",
    "p.legend.orientation = \"horizontal\"\n",
    "p.legend.location = \"top_center\"\n",
    "show(p)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 分组柱状图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('Apples', '2015'),\n",
       " ('Apples', '2016'),\n",
       " ('Apples', '2017'),\n",
       " ('Pears', '2015'),\n",
       " ('Pears', '2016'),\n",
       " ('Pears', '2017'),\n",
       " ('Nectarines', '2015'),\n",
       " ('Nectarines', '2016'),\n",
       " ('Nectarines', '2017'),\n",
       " ('Plums', '2015'),\n",
       " ('Plums', '2016'),\n",
       " ('Plums', '2017'),\n",
       " ('Grapes', '2015'),\n",
       " ('Grapes', '2016'),\n",
       " ('Grapes', '2017'),\n",
       " ('Strawberries', '2015'),\n",
       " ('Strawberries', '2016'),\n",
       " ('Strawberries', '2017')]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from bokeh.models import ColumnDataSource, FactorRange\n",
    "fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']\n",
    "years = ['2015', '2016', '2017']\n",
    "data = {'fruits' : fruits,\n",
    "        '2015'   : [2, 1, 4, 3, 2, 4],\n",
    "        '2016'   : [5, 3, 3, 2, 4, 6],\n",
    "        '2017'   : [3, 2, 4, 4, 5, 3]}\n",
    "x = [ (fruit, year) for fruit in fruits for year in years ]\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "  <div class=\"bk-root\" id=\"494e5a28-b1a7-4480-9f3f-0e016987296b\" data-root-id=\"1485\"></div>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "(function(root) {\n",
       "  function embed_document(root) {\n",
       "    \n",
       "  const docs_json = {\"3d97fe9b-89fe-4f58-b2b3-7322fd32f95c\":{\"defs\":[],\"roots\":{\"references\":[{\"attributes\":{\"below\":[{\"id\":\"1495\"}],\"center\":[{\"id\":\"1497\"},{\"id\":\"1501\"}],\"height\":350,\"left\":[{\"id\":\"1498\"}],\"renderers\":[{\"id\":\"1520\"}],\"title\":{\"id\":\"1486\"},\"toolbar\":{\"id\":\"1509\"},\"x_range\":{\"id\":\"1484\"},\"x_scale\":{\"id\":\"1491\"},\"y_range\":{\"id\":\"1489\"},\"y_scale\":{\"id\":\"1493\"}},\"id\":\"1485\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{\"source\":{\"id\":\"1483\"}},\"id\":\"1521\",\"type\":\"CDSView\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.2},\"fill_color\":{\"value\":\"#1f77b4\"},\"hatch_alpha\":{\"value\":0.2},\"line_alpha\":{\"value\":0.2},\"line_color\":{\"value\":\"#1f77b4\"},\"top\":{\"field\":\"counts\"},\"width\":{\"value\":0.9},\"x\":{\"field\":\"x\"}},\"id\":\"1519\",\"type\":\"VBar\"},{\"attributes\":{},\"id\":\"1496\",\"type\":\"CategoricalTicker\"},{\"attributes\":{\"coordinates\":null,\"group\":null,\"text\":\"Fruit Counts by Year\"},\"id\":\"1486\",\"type\":\"Title\"},{\"attributes\":{\"start\":0},\"id\":\"1489\",\"type\":\"DataRange1d\"},{\"attributes\":{},\"id\":\"1507\",\"type\":\"HelpTool\"},{\"attributes\":{},\"id\":\"1568\",\"type\":\"CategoricalTickFormatter\"},{\"attributes\":{\"tools\":[{\"id\":\"1502\"},{\"id\":\"1503\"},{\"id\":\"1504\"},{\"id\":\"1505\"},{\"id\":\"1506\"},{\"id\":\"1507\"}]},\"id\":\"1509\",\"type\":\"Toolbar\"},{\"attributes\":{},\"id\":\"1571\",\"type\":\"Selection\"},{\"attributes\":{},\"id\":\"1493\",\"type\":\"LinearScale\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"value\":\"#1f77b4\"},\"hatch_alpha\":{\"value\":0.1},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"#1f77b4\"},\"top\":{\"field\":\"counts\"},\"width\":{\"value\":0.9},\"x\":{\"field\":\"x\"}},\"id\":\"1518\",\"type\":\"VBar\"},{\"attributes\":{\"coordinates\":null,\"formatter\":{\"id\":\"1565\"},\"group\":null,\"major_label_policy\":{\"id\":\"1566\"},\"ticker\":{\"id\":\"1499\"}},\"id\":\"1498\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"1502\",\"type\":\"PanTool\"},{\"attributes\":{},\"id\":\"1569\",\"type\":\"AllLabels\"},{\"attributes\":{\"bottom_units\":\"screen\",\"coordinates\":null,\"fill_alpha\":0.5,\"fill_color\":\"lightgrey\",\"group\":null,\"left_units\":\"screen\",\"level\":\"overlay\",\"line_alpha\":1.0,\"line_color\":\"black\",\"line_dash\":[4,4],\"line_width\":2,\"right_units\":\"screen\",\"syncable\":false,\"top_units\":\"screen\"},\"id\":\"1508\",\"type\":\"BoxAnnotation\"},{\"attributes\":{\"factors\":[[\"Apples\",\"2015\"],[\"Apples\",\"2016\"],[\"Apples\",\"2017\"],[\"Pears\",\"2015\"],[\"Pears\",\"2016\"],[\"Pears\",\"2017\"],[\"Nectarines\",\"2015\"],[\"Nectarines\",\"2016\"],[\"Nectarines\",\"2017\"],[\"Plums\",\"2015\"],[\"Plums\",\"2016\"],[\"Plums\",\"2017\"],[\"Grapes\",\"2015\"],[\"Grapes\",\"2016\"],[\"Grapes\",\"2017\"],[\"Strawberries\",\"2015\"],[\"Strawberries\",\"2016\"],[\"Strawberries\",\"2017\"]],\"range_padding\":0.1},\"id\":\"1484\",\"type\":\"FactorRange\"},{\"attributes\":{},\"id\":\"1565\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{},\"id\":\"1503\",\"type\":\"WheelZoomTool\"},{\"attributes\":{\"data\":{\"counts\":[2,5,3,1,3,2,4,3,4,3,2,4,2,4,5,4,6,3],\"x\":[[\"Apples\",\"2015\"],[\"Apples\",\"2016\"],[\"Apples\",\"2017\"],[\"Pears\",\"2015\"],[\"Pears\",\"2016\"],[\"Pears\",\"2017\"],[\"Nectarines\",\"2015\"],[\"Nectarines\",\"2016\"],[\"Nectarines\",\"2017\"],[\"Plums\",\"2015\"],[\"Plums\",\"2016\"],[\"Plums\",\"2017\"],[\"Grapes\",\"2015\"],[\"Grapes\",\"2016\"],[\"Grapes\",\"2017\"],[\"Strawberries\",\"2015\"],[\"Strawberries\",\"2016\"],[\"Strawberries\",\"2017\"]]},\"selected\":{\"id\":\"1571\"},\"selection_policy\":{\"id\":\"1570\"}},\"id\":\"1483\",\"type\":\"ColumnDataSource\"},{\"attributes\":{},\"id\":\"1566\",\"type\":\"AllLabels\"},{\"attributes\":{},\"id\":\"1499\",\"type\":\"BasicTicker\"},{\"attributes\":{\"axis\":{\"id\":\"1498\"},\"coordinates\":null,\"dimension\":1,\"group\":null,\"ticker\":null},\"id\":\"1501\",\"type\":\"Grid\"},{\"attributes\":{\"coordinates\":null,\"formatter\":{\"id\":\"1568\"},\"group\":null,\"major_label_orientation\":1,\"major_label_policy\":{\"id\":\"1569\"},\"ticker\":{\"id\":\"1496\"}},\"id\":\"1495\",\"type\":\"CategoricalAxis\"},{\"attributes\":{},\"id\":\"1570\",\"type\":\"UnionRenderers\"},{\"attributes\":{},\"id\":\"1491\",\"type\":\"CategoricalScale\"},{\"attributes\":{\"axis\":{\"id\":\"1495\"},\"coordinates\":null,\"grid_line_color\":null,\"group\":null,\"ticker\":null},\"id\":\"1497\",\"type\":\"Grid\"},{\"attributes\":{\"fill_color\":{\"value\":\"#1f77b4\"},\"line_color\":{\"value\":\"#1f77b4\"},\"top\":{\"field\":\"counts\"},\"width\":{\"value\":0.9},\"x\":{\"field\":\"x\"}},\"id\":\"1517\",\"type\":\"VBar\"},{\"attributes\":{},\"id\":\"1506\",\"type\":\"ResetTool\"},{\"attributes\":{\"coordinates\":null,\"data_source\":{\"id\":\"1483\"},\"glyph\":{\"id\":\"1517\"},\"group\":null,\"hover_glyph\":null,\"muted_glyph\":{\"id\":\"1519\"},\"nonselection_glyph\":{\"id\":\"1518\"},\"view\":{\"id\":\"1521\"}},\"id\":\"1520\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"overlay\":{\"id\":\"1508\"}},\"id\":\"1504\",\"type\":\"BoxZoomTool\"},{\"attributes\":{},\"id\":\"1505\",\"type\":\"SaveTool\"}],\"root_ids\":[\"1485\"]},\"title\":\"Bokeh Application\",\"version\":\"2.4.2\"}};\n",
       "  const render_items = [{\"docid\":\"3d97fe9b-89fe-4f58-b2b3-7322fd32f95c\",\"root_ids\":[\"1485\"],\"roots\":{\"1485\":\"494e5a28-b1a7-4480-9f3f-0e016987296b\"}}];\n",
       "  root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\n",
       "\n",
       "  }\n",
       "  if (root.Bokeh !== undefined) {\n",
       "    embed_document(root);\n",
       "  } else {\n",
       "    let attempts = 0;\n",
       "    const timer = setInterval(function(root) {\n",
       "      if (root.Bokeh !== undefined) {\n",
       "        clearInterval(timer);\n",
       "        embed_document(root);\n",
       "      } else {\n",
       "        attempts++;\n",
       "        if (attempts > 100) {\n",
       "          clearInterval(timer);\n",
       "          console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\");\n",
       "        }\n",
       "      }\n",
       "    }, 10, root)\n",
       "  }\n",
       "})(window);"
      ],
      "application/vnd.bokehjs_exec.v0+json": ""
     },
     "metadata": {
      "application/vnd.bokehjs_exec.v0+json": {
       "id": "1485"
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "counts = sum(zip(data['2015'], data['2016'], data['2017']), ()) # 分组求和(堆叠总数)\n",
    "source = ColumnDataSource(data=dict(x=x, counts=counts))\n",
    "# 画布\n",
    "p = figure(x_range=FactorRange(*x), plot_height=350, title=\"Fruit Counts by Year\",\n",
    "#            toolbar_location=None, tools=\"\"\n",
    "          )\n",
    "# 柱状图\n",
    "p.vbar(x='x', top='counts', width=0.9, source=source)\n",
    "# 其他\n",
    "p.y_range.start = 0\n",
    "p.x_range.range_padding = 0.1\n",
    "p.xaxis.major_label_orientation = 1\n",
    "p.xgrid.grid_line_color = None\n",
    "# 显示\n",
    "show(p)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 课后实践（star,commits,contributes） 分组柱状图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('matplotlib', 'star'),\n",
       " ('matplotlib', 'commits'),\n",
       " ('matplotlib', 'contributes'),\n",
       " ('bokeh', 'star'),\n",
       " ('bokeh', 'commits'),\n",
       " ('bokeh', 'contributes'),\n",
       " ('pyecharts', 'star'),\n",
       " ('pyecharts', 'commits'),\n",
       " ('pyecharts', 'contributes'),\n",
       " ('seaborn', 'star'),\n",
       " ('seaborn', 'commits'),\n",
       " ('seaborn', 'contributes'),\n",
       " ('plotly', 'star'),\n",
       " ('plotly', 'commits'),\n",
       " ('plotly', 'contributes'),\n",
       " ('ggplot2', 'star'),\n",
       " ('ggplot2', 'commits'),\n",
       " ('ggplot2', 'contributes')]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from bokeh.models import ColumnDataSource, FactorRange\n",
    "xs = ['matplotlib', 'bokeh', 'pyecharts', 'seaborn', 'plotly', 'ggplot2']\n",
    "names = ['star', 'commits', 'contributes']\n",
    "data = {'xs' : xs,\n",
    "        'star'   : [9678, 11034, 5387, 6038, 4928, 3783],\n",
    "        'commits'   : [29503, 17673, 1321, 2316, 3370, 4286],\n",
    "        'contributes'   : [808, 357, 18, 98, 76, 184]}\n",
    "x = [ (x, name) for x in xs for name in names ]\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "  <div class=\"bk-root\" id=\"e222a7b8-bbcf-4056-a2b4-6cc9b1a04b4d\" data-root-id=\"3651\"></div>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "(function(root) {\n",
       "  function embed_document(root) {\n",
       "    \n",
       "  const docs_json = {\"858533bc-0498-4db6-a45e-81b8383298c5\":{\"defs\":[],\"roots\":{\"references\":[{\"attributes\":{\"below\":[{\"id\":\"3661\"}],\"center\":[{\"id\":\"3663\"},{\"id\":\"3667\"}],\"height\":350,\"left\":[{\"id\":\"3664\"}],\"renderers\":[{\"id\":\"3686\"}],\"title\":{\"id\":\"3652\"},\"toolbar\":{\"id\":\"3675\"},\"x_range\":{\"id\":\"3650\"},\"x_scale\":{\"id\":\"3657\"},\"y_range\":{\"id\":\"3655\"},\"y_scale\":{\"id\":\"3659\"}},\"id\":\"3651\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{},\"id\":\"3668\",\"type\":\"PanTool\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.2},\"fill_color\":{\"value\":\"#1f77b4\"},\"hatch_alpha\":{\"value\":0.2},\"line_alpha\":{\"value\":0.2},\"line_color\":{\"value\":\"#1f77b4\"},\"top\":{\"field\":\"counts\"},\"width\":{\"value\":0.9},\"x\":{\"field\":\"x\"}},\"id\":\"3685\",\"type\":\"VBar\"},{\"attributes\":{},\"id\":\"3669\",\"type\":\"WheelZoomTool\"},{\"attributes\":{\"coordinates\":null,\"data_source\":{\"id\":\"3649\"},\"glyph\":{\"id\":\"3683\"},\"group\":null,\"hover_glyph\":null,\"muted_glyph\":{\"id\":\"3685\"},\"nonselection_glyph\":{\"id\":\"3684\"},\"view\":{\"id\":\"3687\"}},\"id\":\"3686\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"bottom_units\":\"screen\",\"coordinates\":null,\"fill_alpha\":0.5,\"fill_color\":\"lightgrey\",\"group\":null,\"left_units\":\"screen\",\"level\":\"overlay\",\"line_alpha\":1.0,\"line_color\":\"black\",\"line_dash\":[4,4],\"line_width\":2,\"right_units\":\"screen\",\"syncable\":false,\"top_units\":\"screen\"},\"id\":\"3674\",\"type\":\"BoxAnnotation\"},{\"attributes\":{\"tools\":[{\"id\":\"3668\"},{\"id\":\"3669\"},{\"id\":\"3670\"},{\"id\":\"3671\"},{\"id\":\"3672\"},{\"id\":\"3673\"}]},\"id\":\"3675\",\"type\":\"Toolbar\"},{\"attributes\":{\"overlay\":{\"id\":\"3674\"}},\"id\":\"3670\",\"type\":\"BoxZoomTool\"},{\"attributes\":{},\"id\":\"3673\",\"type\":\"HelpTool\"},{\"attributes\":{\"axis\":{\"id\":\"3664\"},\"coordinates\":null,\"dimension\":1,\"group\":null,\"ticker\":null},\"id\":\"3667\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"3841\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{},\"id\":\"3665\",\"type\":\"BasicTicker\"},{\"attributes\":{\"coordinates\":null,\"formatter\":{\"id\":\"3841\"},\"group\":null,\"major_label_policy\":{\"id\":\"3842\"},\"ticker\":{\"id\":\"3665\"}},\"id\":\"3664\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"3846\",\"type\":\"UnionRenderers\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"value\":\"#1f77b4\"},\"hatch_alpha\":{\"value\":0.1},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"#1f77b4\"},\"top\":{\"field\":\"counts\"},\"width\":{\"value\":0.9},\"x\":{\"field\":\"x\"}},\"id\":\"3684\",\"type\":\"VBar\"},{\"attributes\":{\"axis\":{\"id\":\"3661\"},\"coordinates\":null,\"grid_line_color\":null,\"group\":null,\"ticker\":null},\"id\":\"3663\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"3671\",\"type\":\"SaveTool\"},{\"attributes\":{\"coordinates\":null,\"group\":null,\"text\":\"\\u4e0d\\u540c\\u5de5\\u5177\\u7684star\\u3001commits\\u3001contributes\\u60c5\\u51b5\"},\"id\":\"3652\",\"type\":\"Title\"},{\"attributes\":{\"start\":0},\"id\":\"3655\",\"type\":\"DataRange1d\"},{\"attributes\":{},\"id\":\"3672\",\"type\":\"ResetTool\"},{\"attributes\":{\"data\":{\"counts\":[9678,29503,808,11034,17673,357,5387,1321,18,6038,2316,98,4928,3370,76,3783,4286,184],\"x\":[[\"matplotlib\",\"star\"],[\"matplotlib\",\"commits\"],[\"matplotlib\",\"contributes\"],[\"bokeh\",\"star\"],[\"bokeh\",\"commits\"],[\"bokeh\",\"contributes\"],[\"pyecharts\",\"star\"],[\"pyecharts\",\"commits\"],[\"pyecharts\",\"contributes\"],[\"seaborn\",\"star\"],[\"seaborn\",\"commits\"],[\"seaborn\",\"contributes\"],[\"plotly\",\"star\"],[\"plotly\",\"commits\"],[\"plotly\",\"contributes\"],[\"ggplot2\",\"star\"],[\"ggplot2\",\"commits\"],[\"ggplot2\",\"contributes\"]]},\"selected\":{\"id\":\"3847\"},\"selection_policy\":{\"id\":\"3846\"}},\"id\":\"3649\",\"type\":\"ColumnDataSource\"},{\"attributes\":{},\"id\":\"3662\",\"type\":\"CategoricalTicker\"},{\"attributes\":{},\"id\":\"3844\",\"type\":\"CategoricalTickFormatter\"},{\"attributes\":{},\"id\":\"3657\",\"type\":\"CategoricalScale\"},{\"attributes\":{\"factors\":[[\"matplotlib\",\"star\"],[\"matplotlib\",\"commits\"],[\"matplotlib\",\"contributes\"],[\"bokeh\",\"star\"],[\"bokeh\",\"commits\"],[\"bokeh\",\"contributes\"],[\"pyecharts\",\"star\"],[\"pyecharts\",\"commits\"],[\"pyecharts\",\"contributes\"],[\"seaborn\",\"star\"],[\"seaborn\",\"commits\"],[\"seaborn\",\"contributes\"],[\"plotly\",\"star\"],[\"plotly\",\"commits\"],[\"plotly\",\"contributes\"],[\"ggplot2\",\"star\"],[\"ggplot2\",\"commits\"],[\"ggplot2\",\"contributes\"]],\"range_padding\":0.1},\"id\":\"3650\",\"type\":\"FactorRange\"},{\"attributes\":{\"source\":{\"id\":\"3649\"}},\"id\":\"3687\",\"type\":\"CDSView\"},{\"attributes\":{},\"id\":\"3845\",\"type\":\"AllLabels\"},{\"attributes\":{},\"id\":\"3842\",\"type\":\"AllLabels\"},{\"attributes\":{\"fill_color\":{\"value\":\"#1f77b4\"},\"line_color\":{\"value\":\"#1f77b4\"},\"top\":{\"field\":\"counts\"},\"width\":{\"value\":0.9},\"x\":{\"field\":\"x\"}},\"id\":\"3683\",\"type\":\"VBar\"},{\"attributes\":{},\"id\":\"3847\",\"type\":\"Selection\"},{\"attributes\":{\"coordinates\":null,\"formatter\":{\"id\":\"3844\"},\"group\":null,\"major_label_orientation\":1,\"major_label_policy\":{\"id\":\"3845\"},\"ticker\":{\"id\":\"3662\"}},\"id\":\"3661\",\"type\":\"CategoricalAxis\"},{\"attributes\":{},\"id\":\"3659\",\"type\":\"LinearScale\"}],\"root_ids\":[\"3651\"]},\"title\":\"Bokeh Application\",\"version\":\"2.4.2\"}};\n",
       "  const render_items = [{\"docid\":\"858533bc-0498-4db6-a45e-81b8383298c5\",\"root_ids\":[\"3651\"],\"roots\":{\"3651\":\"e222a7b8-bbcf-4056-a2b4-6cc9b1a04b4d\"}}];\n",
       "  root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\n",
       "\n",
       "  }\n",
       "  if (root.Bokeh !== undefined) {\n",
       "    embed_document(root);\n",
       "  } else {\n",
       "    let attempts = 0;\n",
       "    const timer = setInterval(function(root) {\n",
       "      if (root.Bokeh !== undefined) {\n",
       "        clearInterval(timer);\n",
       "        embed_document(root);\n",
       "      } else {\n",
       "        attempts++;\n",
       "        if (attempts > 100) {\n",
       "          clearInterval(timer);\n",
       "          console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\");\n",
       "        }\n",
       "      }\n",
       "    }, 10, root)\n",
       "  }\n",
       "})(window);"
      ],
      "application/vnd.bokehjs_exec.v0+json": ""
     },
     "metadata": {
      "application/vnd.bokehjs_exec.v0+json": {
       "id": "3651"
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "counts = sum(zip(data['star'], data['commits'], data['contributes']), ()) # 分组求和(堆叠总数)\n",
    "source = ColumnDataSource(data=dict(x=x, counts=counts))\n",
    "# 画布\n",
    "p = figure(x_range=FactorRange(*x), plot_height=350, title=\"不同工具的star、commits、contributes情况\"\n",
    "#            toolbar_location=None, tools=\"\"\n",
    "          )\n",
    "# 柱状图\n",
    "p.vbar(x='x', top='counts', width=0.9, source=source)\n",
    "# 其他\n",
    "p.y_range.start = 0\n",
    "p.x_range.range_padding = 0.1\n",
    "p.xaxis.major_label_orientation = 1\n",
    "p.xgrid.grid_line_color = None\n",
    "# 显示\n",
    "show(p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6rc1"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
